<?php
class FeedsourcesController extends AppController 
  {
    public $helpers = array('Time','Html','Form','Ajax');
    public function index() 
    {
        $this->paginate = array(
            'url' => 'http://vnexpress.net/rss/gl/vi-tinh.rss',
            'order' => array('pubDate' => 'desc'),
            'limit' => 9
        );
      $this->set('feeds', $this->paginate());
    }
    
    function saveFeeds($id = null) {
        $feed = $this->Feedsource->Feed->read(null, $id);
        
        if (!$feed["Feed"]["url"]) {
            $this->Session->setFlash(__('Invalid feed', true));
            //$this->redirect(array('action' => 'index'));
	}
        $this->paginate = array(
            'url' => $feed["Feed"]["url"],
            'order' => array('pubDate' => 'desc'),
            'limit' => 20
        );
	
	$feeds = $this->paginate();
	
	$count = 0;
	foreach($feeds as $f)
	{
	  $feeddetail["FeedDetail"]["title"] = $f["Feedsource"]["title"];
	  $feeddetail["FeedDetail"]["link"] = $f["Feedsource"]["link"];
	  $feeddetail["FeedDetail"]["pub_date"] = $f["Feedsource"]["pubDate"];
	  $feeddetail["FeedDetail"]["description"] = $f["Feedsource"]["description"];
	  $feeddetail["FeedDetail"]["status"] = 0;
	  $feeddetail["FeedDetail"]["feed_id"] = $feed["Feed"]["id"];
	  
	  //save to db
	  $exist = $this->Feedsource->FeedDetail->find('first', array('conditions' => array('FeedDetail.link' => $f["Feedsource"]["link"])));
	  //var_dump($exist)."sdfsdfsdfs";
	  if(!$exist)
	  {
	    $this->Feedsource->FeedDetail->create();
	    $this->Feedsource->FeedDetail->save($feeddetail);
	    $count++;
	  }
	  else
	  {
	    break;
	  }
	}
        
        $this->set('feeds', $feeds);
	$this->set('feed', $feed);
	$this->set('count', $count);
    }
    
    function saveItemsViaUrl($id = null)
    {
      $feed = $this->Feedsource->Feed->read(null, $id);
      $data = file_get_contents($feed["Feed"]["url"]);
      //echo $data;
      
      
      $order   = array("\r\n", "\n", "\r", "\t");
      $replace = '';
      // Processes \r\n's first so they aren't converted twice.
      $data = str_replace($order, $replace, $data);
      
      //lay link hinh
      if(preg_match('/(.*?)vnexpress.net(.*?)/', $feed["Feed"]["url"])) {
	preg_match_all( '/class\=\"folder(.*?)href\=\"(.*?)\"(.*?)class\=\"link(.*?)\>(.*?)\<(.*?)class\=\"item-time\"\>(.*?)\<(.*?)class\=\"item-date\"\>(.*?)\</', $data, $match );


	$links = $match[2];
	$titles = $match[5];
	$dates = $match[9];
	$times = $match[7];
	$count = 0;
	$result = "";
      }
      else if(preg_match('/(.*?)bongda.com.vn(.*?)/', $feed["Feed"]["url"]))
      {
	preg_match_all( '/\<h2 rel=\'rnd\'>(.*?)href\=\"(.*?)\"(.*?)\>(.*?)\<(.*?)class=\'fshaded\'\>(.*?)\</', $data, $match );
	//var_dump($match[6]);
	$links = $match[2];
	$titles = $match[4];
	$dates = $match[6];
	$times = array('','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','');

	$count = 0;
	$result = "";
      }
      
      foreach($links as $key => $link){
	//sua link hinh
	  //Get root url
	  preg_match( '/http\:\/\/(.*?)\//', $feed["Feed"]["url"], $matchurl );
	  //echo $match[0];
	  $root_url = $matchurl[0];     
	  //root link
	  $link = preg_replace('/^\/+/', $root_url, $link);
	  
	  
	  
	  if($titles[$key] != ""){
	    $feeddetail["FeedDetail"]["title"] = $titles[$key];
	    $feeddetail["FeedDetail"]["link"] = $link;
	    $feeddetail["FeedDetail"]["pub_date"] = $times[$key].$dates[$key];    
	    $feeddetail["FeedDetail"]["status"] = 0;
	    $feeddetail["FeedDetail"]["feed_id"] = $feed["Feed"]["id"];
	    
	    //save to db
	    $exist = $this->Feedsource->FeedDetail->find('first', array('conditions' => array('FeedDetail.link' => $link)));
	    //var_dump($exist)."sdfsdfsdfs";
	    if(!$exist)
	    {
	      $this->Feedsource->FeedDetail->create();
	      $this->Feedsource->FeedDetail->save($feeddetail);
	      $count++;
	    }
	    else
	    {
	      break;
	    }
	    $result .= $titles[$key]."<br />".$link."<br />".$times[$key].$dates[$key]."<br />";
	  }
      }
      
      
      
      
      
      $this->set('feed', $feed);
      $this->set('count', $count);
      $this->set('result', $result);
    }
    
    function downloadContents($id = null)
    {
      $feed = $this->Feedsource->Feed->read(null, $id);
      
      $feeddetails = $this->Feedsource->FeedDetail->find('all', array('conditions' => array('FeedDetail.feed_id' => $feed["Feed"]["id"])));
      
      //echo count($feeddetails);
      
      $count = 0;
      $result = "";
      foreach($feeddetails as $ft)
      {
	if(!$ft["FeedDetail"]["status"])
	{
	  $content = $this->downloadContent($ft["FeedDetail"]["link"]);
	  //echo $content;
	  //return;
	  //echo $ft["FeedDetail"]["title"];
	  $ft["FeedDetail"]["content"] = $content;
	  $ft["FeedDetail"]["status"] = 1;
	  $feed = $this->Feedsource->FeedDetail->save($ft);
	  $count++;
	  $result .= $ft["FeedDetail"]["title"]."<br />";
	}
      }
      $this->set('feed', $feed);
      $this->set('count', $count);
      $this->set('result', $result);
    }
    
    function downloadContent($url = null)
    {
	//echo $url;
	//$url = 'http://vnexpress.net/gl/vi-tinh/2011/09/nhung-chieu-lua-qua-quang-cao-google/';
        $data = file_get_contents($url);
               
        $name = 'hello <body>kl<div>kekeke</div></body>';
        
	
	$order   = array("\r\n", "\n", "\r", "\t");
	$replace = '';

	// Processes \r\n's first so they aren't converted twice.
	$newstr = str_replace($order, $replace, $data);
	
	
        //preg_match( '/\<body\>(.*?)\<\/body\>/', $newstr, $match );
	
	
	//preg VNExpress.net
	if(preg_match('/(.*?)vnexpress.net(.*?)/', $url)) {
	  preg_match( '/\<div class="content"\>(.*?)\<\/div\>\<div class="tag\-parent"\>/', $newstr, $match );
	  $result = $match[1];
	}
	else if(preg_match('/(.*?)bongda.com.vn(.*?)/', $url))
	{
	  preg_match( '/\<div align=\'justify\'\>(.*?)\<\/div\>\<div align\=\'right\'\>(.*?)\<\/div\>\<div class\=\'spacer1\'>/', $newstr, $match );
	  //var_dump($match);
	  $result = $match[1].$match[2];
	}
	
        //var_dump( $match );
	//print_r(htmlentities($data));
        //echo htmlentities($match[1]);
	//echo htmlentities($newstr);
        //echo $match[1];
	
	
	
	//Fix images link
	$result = $this->fixImageLink($result, $url);
		
	//clean HTML
	$result = $this->cleanHTML($result);
	
	
	$article["HkContent"]["fulltext"] = $result;
	//$article["HkContent"]["fulltext"] = $result;
	//$article["HkContent"]["fulltext"] = $result;
	//$article["HkContent"]["fulltext"] = $result;
	//$article["HkContent"]["fulltext"] = $result;
	//$article["HkContent"]["fulltext"] = $result;
	//$article["HkContent"]["fulltext"] = $result;
	//$article["HkContent"]["fulltext"] = $result;
	//$article["HkContent"]["fulltext"] = $result;
	
	
	$this->Feedsource->HkContent->save($article);
	
	
	//$this->set('result', $result);
	return $result;
    }
    
    function cleanHTML($data)
    {
	//bo loc
	$old = array("</div>", "</p>", "</h1>", "</h2>", "<br />", "<br>", "</DIV>", "</P>", "</H1>", "</H2>", "<BR />", "<BR>", "</tr>");
	$new = array("zzz", "zzz", "zzz", "zzz", "zzz", "zzz", "zzz", "zzz", "zzz", "zzz", "zzz", "zzz", "zzz",);
	
	//$old = array("<h2", "</h2>", "<H2", "</H2>");
	//$new = array("<strong", "</strong>", "<strong", "</strong>");
	
	$result = str_replace($old, $new, $data);
	//remove title
	$result = preg_replace('/\<H1(.*?)\<\/H1\>/', '', $result);
	$result = preg_replace('/\<h1(.*?)\<\/h1\>/', '', $result);
	$result = preg_replace('/\<div(.*?)email-print(.*?)\<\/div\>/', '', $result);
	$result = preg_replace('/\<img/', "<img  style='margin-left:auto;margin-right:auto;display:block' ", $result);
	$result = str_replace("<IMG", '<img style="margin-left:auto;margin-right:auto;display:block" ', $result);
	$result = str_replace("<td", '<td align="center" ', $result);
	$result = str_replace("<TD", '<td align="center" ', $result);
	$result = strip_tags($result, '<img><p><strong>');	
	$result = preg_replace('/zzz+/', "<br />", $result);
	$result = preg_replace('/^(\<br \/\>)+/', "", $result);
	
	return $result;
    }
    
    function fixImageLink($data, $url)
    {
      //Get root url
	preg_match( '/http\:\/\/(.*?)\//', $url, $match );
	//echo $match[0];
	$root_url = $match[0];
      
      
      //root link
      $result = preg_replace('/src\=\"\/+/', 'src="'.$root_url, $data);
      $result = preg_replace('/href\=\"\/+/', 'href="'.$root_url, $result);
      
      
      return $result;
    }
    
    
    
    function importArticles($id = null)
    {
      $feed = $this->Feedsource->Feed->read(null, $id);
      
      $feeddetails = $this->Feedsource->FeedDetail->find('all', array('conditions' => array('FeedDetail.feed_id' => $feed["Feed"]["id"], 'FeedDetail.status' => 1)));
      
      //echo count($feeddetails);
      
      $count = 0;
      $result = "";
      foreach($feeddetails as $ft)
      {
	if($ft["FeedDetail"]["status"] == 1)
	{
	  $article["HkContent"]["introtext"] = $ft["FeedDetail"]["content"];
	  $article["HkContent"]["title"] = $ft["FeedDetail"]["title"];
	  $article["HkContent"]["alias"] = str_replace(" ", "-", $ft["FeedDetail"]["title"]);
	  $article["HkContent"]["state"] = 1;
	  $article["HkContent"]["catid"] = $feed["Feed"]["hk_category_id"];
	  $article["HkContent"]["created"] = date('Y-m-d 00:00:00');
	  $article["HkContent"]["publish_up"] = date('Y-m-d 00:00:00');
	  $article["HkContent"]["access"] = 1;
	  $article["HkContent"]["language"] = "*";
	  $article["HkContent"]["created_by"] = $feed["Feed"]["hk_user_id"];;
	  
	  if($article["HkContent"]["introtext"])
	  {
	    $this->Feedsource->HkContent->create();
	    $this->Feedsource->HkContent->save($article);
	  }
	  
	  //echo $this->Feedsource->HkContent->getLastInsertId();
	  
	  $count++;
	  $ft["FeedDetail"]["status"] = 2;
	  $ft["FeedDetail"]["hk_content_id"] = $this->Feedsource->HkContent->getLastInsertId();
	  $this->Feedsource->FeedDetail->save($ft);
	}
      }
      $this->set('feed', $feed);
      $this->set('count', $count);
      $this->set('result', $result);
    }
  }
?>